<?php
namespace app\web\library;

use app\admin\plug\adminPower;
use app\admin\library\User;
use cubox\App;
class PartPower{

    private App $app;
    public string $adminPart = '';
    public string $adminArticlePart = '';
    public function __construct(App $app){
        $this->adminPart = User::getPowerExtend('web','web.part','adminPart')??"";
        $this->adminArticlePart = User::getPowerExtend('web','web.article','adminArticlePart')??'';
        $this->app = $app;
    }


    public function partSuper(){
        if (User::super() || $this->adminPart == 'all'){
            return true;
        }
    }

    public function articlePartSuper(){
        if (User::super() || $this->adminArticlePart == 'all'){
            return true;
        }
    }



    public function inPart($id){
        
        if ($this->partSuper()) return true;

        if ($id == 0 ) return true;

        $list = $this->getPartList();

        if (in_array($id,$list)){
            return true;
        }else{
            return false;
        }
    }

    public function inArticlePart($id){
        
        if ($this->articlePartSuper()) return true;

        $list = $this->getArticlePartList();

        if (in_array($id,$list)){
            return true;
        }else{
            return false;
        }
    }


    public function getPartList(){
        $data = User::get('web-partlist');
        
        if ($data){
            return $data;
        }else{
            $partList = [];

            if ($this->adminPart=='custom'){
                $partList = User::getPowerExtend('web','web.part','adminPartList',[]);
            }
            
            $list = [];
            if (count($partList)>0){

                $part = $this->app->model('Part');

                foreach($partList as $Item){
                    if (!in_array($Item['part_id'],$list)){
                        $list = array_merge($list,$part->getKeyCollect($Item['part_id']));
                    }    
                }
            }

            User::set('web-partlist',$list);
            return $list;
        }
    }

    public function getArticlePartList(){
        $data = User::get('web-article-partlist');
        
        if ($data){
            return $data;
        }else{
            $partList = [];

            if ($this->adminArticlePart=='custom'){
                $partList = User::getPowerExtend('web','web.article','adminArticlePartList',[]);
            }
            
            $list = [];
            if (count($partList)>0){

                $part = $this->app->model('Part');

                foreach($partList as $Item){
                    if (!in_array($Item['part_id'],$list)){
                        $list = array_merge($list,$part->getKeyCollect($Item['part_id']));
                    }    
                }
            }

            User::set('web-article-partlist',$list);
            return $list;
        }
    }

}